
package com.kanjian.music.fragment;

import java.util.ArrayList;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.adapter.MusicListListAdapater;
import com.kanjian.music.entity.Music;
import com.kanjian.music.util.PlayerUtil;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class MusicPlayerMusicListFragment extends BaseFragment {

    public static MusicPlayerMusicListFragment newInstance() {
        return new MusicPlayerMusicListFragment();
    }

    private View mRootView;

    private ListView mLv_musicList;

    private MusicListListAdapater mListAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return mRootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_musicplayer_musiclist, container, false);
            mLv_musicList = (ListView) mRootView
                    .findViewById(R.id.music_player_music_list_listview);
        }
        refershMusicList();
    }

    public void refershMusicList() {
        if (mListAdapter == null) {
            mListAdapter = new MusicListListAdapater((BaseActivity) getActivity(), mLv_musicList,
                    new ArrayList<Music>(PlayerUtil.playerList));
        }
        if (mLv_musicList.getAdapter() == null) {
            mLv_musicList.setAdapter(mListAdapter);
        } else {
            mListAdapter.setMusicList(new ArrayList<Music>(PlayerUtil.playerList));
            mListAdapter.notifyDataSetChanged();
        }
    }

}
